<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @clear="clearct" />
      <el-button type="primary" @click="getct">查询</el-button>
    </div>
    <el-dialog
      :title="isEdit?'添加楼宇':'编辑楼宇'"
      :visible="dialogVisible"
      width="30%"
      @close="handleClose"
    >
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmqd">确 定</el-button>
      </span>
    </el-dialog>
    <el-button type="primary" @click="upDialog">添加楼宇</el-button>
    <el-button @click="exportToExcel">导出Excel</el-button>

    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="tableList"
      >
        <el-table-column
          type="index"
          label="序号"
        />
        <el-table-column
          label="楼宇名称"
          width="180"
          prop="name"
        />
        <el-table-column
          label="层数"
          prop="floors"
        />
        <el-table-column
          label="在管面积(m²)"
          width="120"
          prop="area"
        />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column
          label="状态"
          prop="status"
        >
          <template #default="scope">{{ scope.row.status===0?'租赁中':'闲置中' }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="end(scope.row)"
            >编辑</el-button>
            <el-button
              :disabled="!scope.row.status"
              size="mini"
              type="text"
              @click="confirmDel(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-container">
        <el-pagination
          layout="total, prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { utils, writeFileXLSX } from 'xlsx'
import { delBuildingListAPI, getBuildingAPI, createBuildingListAPI, modificationAPI, xiangQingAPI } from '@/api/enterprise'
export default {
  name: 'Building',
  data() {
    return {
      params: {
        page: 1,
        pageSize: 5,
        name: ''
      },
      isEdit: false,
      tableList: [],
      total: 0,
      dialogVisible: false,
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入楼宇面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入楼宇物业费', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getct()
  },
  methods: {
    // 删除
    async confirmDel(id) {
      this.$confirm('确认删除当前楼宇吗？',
        '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
        await delBuildingListAPI(id)
        this.getct()
      }).catch(() => {

      })
    },
    async getct() {
      const res = await getBuildingAPI(this.params)
      this.total = res.data.total
      this.tableList = res.data.rows
      // console.log(res)
    },
    clearct() {
      this.getct()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getct()
    },
    upDialog() {
      this.dialogVisible = true
      this.isEdit = true
    },
    handleClose() {
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
      this.isEdit = false
    },
    // 添加
    confirmqd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.addForm.id) {
            await modificationAPI(this.addForm)
          } else {
            await createBuildingListAPI(this.addForm)
          }
          this.getct()
          this.dialogVisible = false
        }
      })
    },
    async end(row) {
      this.dialogVisible = true
      const res = await xiangQingAPI(row.id)
      // 解构赋值
      const { id, area, floors, name, propertyFeePrice } = res.data
      this.addForm = {
        id, area, floors, name, propertyFeePrice
      }
    },
    async exportToExcel() {
      const res = await getBuildingAPI(this.params)
      const tableHeader = ['name', 'floors', 'area', 'propertyFeePrice', 'status']
      const sheetdate = res.data.rows.map((item) => {
        const obj = {}
        tableHeader.forEach(key => {
          obj[key] = item[key]
        })
        return obj
      })
      // 创建一个新的工作簿
      const workbook = utils.book_new()
      // 创建一个工作表
      const worksheet = utils.json_to_sheet(sheetdate)

      // 把工作表添加到工作簿  Data为工作表名称
      utils.book_append_sheet(workbook, worksheet, 'Data')
      // 改写表头
      utils.sheet_add_aoa(worksheet, [['楼宇名称', '层数', '在管面积(㎡)', '物业费(㎡)', '状态']], { origin: 'A1' })
      writeFileXLSX(workbook, 'SheetJSVueAoO.xlsx')
    }
  }}
</script>

<style lang="scss" scoped>
.department-container{
  padding:10px;
}
.search-container{
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237,237,237,.9);;
  padding-bottom: 20px;
  .search-label{
    width:100px;
  }
  .search-main{
    width: 220px;
    margin-right: 10px;
  }
.page-container{
  padding:4px 0px;
  text-align: right;
  }
}
</style>
